<template>
  <div class="building">
    <div class="login-box">
      <router-view></router-view>
      <h1>登录</h1>
      <span class="error">{{error}}</span>

      <el-form
        :model="loginForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="40px"
        class="demo-ruleForm"
      >
        <el-form-item label="账号" prop="username">
          <el-input
            type="text"
            v-model="loginForm.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="loginForm.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>

          <el-select v-model="formInline.region" placeholder="活动区域">
        <el-option label="我是医生" value="d"></el-option>
        <el-option label="我是管理员" value="a"></el-option>
      </el-select>
      <br>
      <br>

        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>


    </div>
  </div>
</template>
<script>
import jwt from 'jwt-decode'
export default {
  data() {
    var validateUser = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名"));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [{ validator: validateUser, trigger: "blur" }],
        password: [{ validator: validatePass, trigger: "blur" }],
      },
      formInline: {
          region: '选择账户权限',
      },
      error:"",
      visible: true,
    };
  },
  methods: {
    //下拉框触发的方法
    handleCommand() {
      console.log(this.formInline.region)
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //前端验证通过，调用后端登录接口,判断是否登录成功
          if(this.formInline.region == '选择账户权限'){
            alert("选择账户权限");
          }else{
          this.$axios.post("http://localhost:8080/"+this.formInline.region+"/login",this.loginForm).then(src=>{
            // 用户名不存在或者密码错误的捕获
            if(src.data.code==422){
              this.error = src.data.msg
            }
            if(src.data.code==200){
              //存储登录信息

              //解析token获取部门信息
              let role = jwt(src.headers.token).role;
              //封装需要存储的数据
              let user = {
                username:jwt(src.headers.token).username,
                token:src.headers.token,
                path:role,
              }
              //持久化
              localStorage.setItem("user",JSON.stringify(user))

              this.$router.push("/"+role)
            }
          })
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style lang="less" scoped>
.login-box {
  width: 400px;
  height: 340px;
  padding: 20px;
  margin: 150px auto;
  border-radius: 10px;
  border: 1px solid #eee;
  background-color: rgba(222, 222, 221, 0.5);
}
.building {
  background: url("../../assets/image/logo.jpeg");
  width: 100%;
  height: 100%;
  position: fixed;
  background-size: 100% 100%;
}
.error{
  color: red;
}
</style>
